package com.jxnd.sunflower.config;

import com.jxnd.sunflower.domain.dto.UserDTO;
import com.jxnd.sunflower.mapper.RUserMapper;
import com.jxnd.sunflower.utils.JwtToken;
import com.jxnd.sunflower.utils.LocalUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: 敷衍
 * @description:
 * @date: 2021/12/5 15:17
 * @version: 1.0
 */
public class UserTokenInterceptor  implements HandlerInterceptor {


    @Autowired
    private RUserMapper rUserMapper;

    /**
     * 拦截请求 访问controller之前
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取前端的值


        String token = request.getHeader("Authorization");
        if(token.length()>0) {
            System.out.println(token);
//            Boolean run = JwtToken.verifierToken(token);
//            Integer userId = JwtToken.getUserId("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MzkxNDY3NDh9.ll-Yh2ELKj-kNwnPjqgwGGQ1YVorNLDrZ6F17Je9iTo");
//            System.out.println(userId);
//            if(userId!=null){
//                UserDTO userDTO = rUserMapper.queryUserInfo(userId);
//                LocalUser.set(userDTO);
//            }

        }




        /**
         * false： 请求被拦截
         * true: 请求通过验证，放行
         */

        return true;
    }


    /**
     * 请求访问到controller之后 渲染视图之前
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }


    /**
     * 请求访问到controller之后 渲染视图之后
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
